package com.kfm.company.mapper;

import com.kfm.company.model.Goods;
import com.kfm.company.model.Repertory;
import com.kfm.company.provider.GoodsProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {

    /**
     * '@Select("select * from goods")' --> <select></select>
     * '@Results(id="goodsResult",...)' --> <resultMap></resultMap>
     * '@Result(id = true, ....)' --> <id></id>
     * '@Result()' --> <result></result>
     * '@ResultMap("goodsResult")' --> <select resultMap = "goodsResult"></select>
     *
     * @return
     */

    @Results(id = "goodsResult",
            value = {
                    @Result(id = true, property = "id", column = "id"),
                    @Result(property = "name", column = "goods_name"),
                    @Result(property = "price", column = "goods_price"),
                    @Result(property = "categoryId", column = "category_id"),
            }
    )
    // @Select("select * from goods")
    // value--> 指定的提供sql的类，method-->指定提供sql的方法
    @SelectProvider(value = GoodsProvider.class, method = "selectAll")
    List<Goods> selectAll();


    // @Select("<script>" +
    //         "select * from goods " +
    //         "<where>" +
    //         "<if test='id != null'>" +
    //         "and id = #{id}" +
    //         "</if>" +
    //         "<if test='name != null'>" +
    //         "and goods_name = #{name}" +
    //         "</if>" +
    //         "<if test='price != null'>" +
    //         "and goods_price = #{price}" +
    //         "</if>" +
    //         "<if test='categoryId != null'>" +
    //         "and category_id = #{categoryId}" +
    //         "</if>" +
    //         "</where>" +
    //         "</script>"
    // )
    @SelectProvider(type = GoodsProvider.class, method = "selectByGoods")
    List<Goods> selectByGoods(Goods goods);


    @Select("select * from goods where id = #{id}")
    @ResultMap("goodsResult")
    Goods selectById(Integer id);

    @Insert("insert into goods(goods_name, goods_price) values(#{name}, #{price})")
    int insert(Goods goods);


    // @Insert("<script>" +
    //         "insert into goods" +
    //         "<trim prefix='(' suffixOverrides=',' suffix=')'>" +
    //         "<if test='id != null'>" +
    //         "id," +
    //         "</if>" +
    //         "<if test='name != null'>" +
    //         "goods_name," +
    //         "</if>" +
    //         "<if test='price != null'>" +
    //         "goods_price," +
    //         "</if>" +
    //         "<if test='categoryId != null'>" +
    //         "category_id," +
    //         "</if>" +
    //         "</trim>" +
    //         "<trim prefix='values(' suffixOverrides=',' suffix=')'>" +
    //         "<if test='id != null'>" +
    //         "#{id}," +
    //         "</if>" +
    //         "<if test='name != null'>" +
    //         "#{name}," +
    //         "</if>" +
    //         "<if test='price != null'>" +
    //         "#{price}," +
    //         "</if>" +
    //         "<if test='categoryId != null'>" +
    //         "#{categoryId}," +
    //         "</if>" +
    //         "</trim>" +
    //         "</script>")
    @InsertProvider(type = GoodsProvider.class, method = "insert2")
    int insert2(Goods goods);
    /**
     * 获取插入的id
     *
     * @param goods
     * @return
     */
    // @Insert("insert into goods(goods_name, goods_price) values(#{name}, #{price})")
    // @SelectKey(keyColumn = "id", keyProperty = "id", resultType = Integer.class,
    //         statement = "select last_insert_id()", before = false)
    // int insert2(Goods goods);

    // @Insert("insert into goods(goods_name, goods_price) values(#{name}, #{price})")
    // @SelectKey(keyColumn = "id", keyProperty = "id", resultType = String.class,
    //         statement = "select replace(uuid(),'-','')", before = true)
    // int insert3(Goods goods);

    /**
     * '@Delete()' id 默认是方法名 --> <delete id="delete"></delete>
     *
     * @param id
     * @return
     */
    @Delete("delete from goods where id = #{id}")
    int deleteById(Integer id);

    @Update("update goods set goods_name = #{name} , goods_price = #{price} where id = #{id}")
    int update(Goods goods);

    @UpdateProvider(value = GoodsProvider.class, method = "updateByGoods")
    int updateByGoods(Goods goods);

    @Select("select count(*) from goods")
    int getTotal();

    /**
     * 查询所有商品信息并且拥有商品的库存信息
     * '@One' --> <association colum+select></association>
     *
     * @return
     */
    @Select("select * from goods")
    @Results(id = "goodsResult2",
            value = {
                    @Result(id = true, column = "id", property = "id"),
                    @Result(column = "goods_name", property = "name"),
                    @Result(column = "goods_price", property = "price"),
                    @Result(property = "categoryId", column = "category_id"),
                    @Result(property = "repertory", column = "id", javaType = Repertory.class, one = @One(
                            select = "com.kfm.company.mapper.RepertoryMapper.selectByGoodsId"
                    ))
            })
    List<Goods> selectAll2();

    @Select("select * from goods where category_id = #{cid}")
    @ResultMap("goodsResult")
    List<Goods> selectByCategoryId(Integer cid);

}
